<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/4/28
 * Time: 16:34
 */

namespace app\api\controller\user;


use app\api\controller\BasicUserApi;
use app\store\service\OrderService;
use think\Db;

class Exchange extends BasicUserApi
{
    /**
     * 结算页面
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function settlement(){
        $post=$this->request->only(['id','number'],'post');
        $data=OrderService::confirmOrder($post);
        if($data['code']==0){
            return json(['msg'=>$data['msg']]);
        }
        //查询默认收货地址
        $address=Db::name('store_member_address')
            ->where('mid',UID)
            ->where('is_default',1)
            ->field('id,username,phone,area,address')
            ->find();

        return json(['msg'=>'success','data'=>[
            'address'=>$address,
            'goods'=>$data['data']
        ]]);
    }

    /**
     * 创建并支付订单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function createorder(){
        $post=$this->request->only(['id','number','address_id','remark'],'post');

        $params=$post['id'].'@default:default@'.$post['number'];
        $data=OrderService::create(UID,$params,$post['address_id'],'',$post['remark'],2);
        if($data['code']==0){
            return json(['msg'=>$data['msg']],500);
        }
        //积分支付
        $data=OrderService::point_pay(UID,$data['order_no']);
        if($data['code']==0){
            return json(['msg'=>$data['msg']],500);
        }
        return json(['msg'=>$data['msg']]);
    }
}